﻿//先不管是ie几,把所有js文件都依赖一下
define(["jquery", "jqueryMigrate", "bootstrap", "bootstrapHoverDropdown",
    "jquerySlimscroll", "jqueryBlockUi", "jqueryCokie", "jqueryUniform",
    "select2", "jqueryDataTables", "DtBootstrap", "app", "msgBox", "init", "comm"], function ($) {

        var appList = function () {

            var initTable1 = function (tableId, hideColumns) {
                if (!!!tableId) {
                    alert('tableId 錯誤');
                    return;
                }
                if (!!!tableId.startsWith('#')) {
                    tableId = '#' + tableId;
                }

                var table = $(tableId);
                if (!!!table) {
                    return;
                }
                var wrapId = tableId + '_wrapper';

                var oTable = $(table).dataTable({
                    //隐藏列
                    "aoColumnDefs": [
                        { "bSortable": false, "aTargets": [0] },
                        { "bSortable": false, "aTargets": [8] },
                        { "bSortable": false, "aTargets": [9] },
                    ],
                    "aaSorting": [[2, 'asc']],
                    "aLengthMenu": [
                       [5, 15, 20, -1],
                       [5, 15, 20, "All"] // change per page values here
                    ],
                    // set the initial value
                    "iDisplayLength": 10,
                });

                var wrapdiv = $(wrapId);
                $(wrapdiv).find('.dataTables_filter input').addClass("form-control input-small"); // modify table search input
                $(wrapdiv).find('.dataTables_length select').addClass("form-control input-small"); // modify table per page dropdown
                $(wrapdiv).find('.dataTables_length select').select2(); // initialize select2 dropdown


                $(table).find('.group-checkable').change(function () {

                    var set = $(this).attr("data-set");
                    var checked = $(this).is(":checked");
                    if (checked) {
                        $(this).parents('span').addClass('checked');
                    } else {
                        $(this).parents('span').removeClass('checked');
                    }
                    $(set).each(function () {
                        if (checked) {
                            $(this).prop("checked", true);
                            $(this).parents('tr').addClass("active");
                            $(this).parents('span').addClass('checked');
                        } else {
                            $(this).prop("checked", false);
                            $(this).parents('tr').removeClass("active");
                            $(this).parents('span').removeClass('checked');
                        }
                    });
                    $.uniform.update(set);
                });

                //單獨點擊某個按鈕時
                $(table).find('tbody tr .checkboxes').change(function () {
                    $(this).parents('tr').toggleClass("active");
                    $(this).parents('span').toggleClass('checked');
                });
            }

            var deleteList = function deleteList() {
                $('#' + tableId + ' thead .fa.fa-trash-o').on('click', function () {
                    var arr = [];
                    $(checkboxDataSet + ':checked')
                        .each(function (i, e) {
                            arr.push($(e).val());
                        });

                    var data = { "ids": arr };

                    $.post(deleteUrl,
                        data,
                        function (result) {
                            msgBox.showMsg(result);
                            comm.ajaxSuccess(result);
                        },
                        "json");
                });
            }
            return {

                //main function to initiate the module
                init: function (tableId, hideColumns) {

                    if (!$().dataTable) {
                        return;
                    }

                    initTable1(tableId);
                },
                deleteList: function () {
                    deleteList();
                }
            };

        }();

        $(function () {
            appList.init(tableId);
            appList.deleteList();
        });


    });
